home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / PalmBoxer / docompr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.1 KB  |  98 lines

  1. #define IGNORE_STDIO_STUBS
  2. #define __string_h
  3.  
  4. #ifdef OLDGCC
  5.  
  6. #include <Common.h>
  7. #include <System/SysAll.h>
  8. #include <UI/UIAll.h>
  9.  
  10. #else
  11.  
  12. #include <PalmOS.h>
  13. #include <PalmCompatibility.h>
  14.  
  15. #endif
  16.  
  17. #include <Unix/sys_types.h>
  18.  
  19. int compcore(unsigned char **matat, unsigned char *head, int max);
  20.  
  21. int compdoc(unsigned char *ibuf, int ilen, unsigned char *obuf, int quick)
  22. {
  23.   int space = 0; /* set for pending space */
  24.   int nhc = 0; /* number of literals in block (so far) */
  25.   int mlen, max; /* for repeated section compression */
  26.  
  27.   unsigned char *matat; /* match at pointer - where to begin search and where found */
  28.   unsigned char *iptr = ibuf, *iend = ibuf + ilen, *optr = obuf; /* working pointers */
  29.   unsigned char c; /* faster than *iptr everywhere */
  30.  
  31.   /* 2047 is the maximum displacement,
  32.      shifting narrows the search (it is an N**2 term, so 1023 is almost 4x faster) */
  33.  
  34.   quick = 2047 >> quick;
  35.  
  36.   while (iptr != iend) {
  37.  
  38.     matat = iptr - ibuf > quick ? iptr - quick : ibuf;
  39.  
  40.     max = iend - iptr > 11 ? 11 : iend - iptr;
  41.  
  42.     /* check beginning and end cases, then test for a repeat match */
  43.     if (max > 3 && matat != iptr && (mlen = compcore(&matat, iptr, max)) > 1 ) {
  44.       unsigned int uw16;
  45.  
  46.       nhc = 0;
  47.       if (space)
  48.     *optr++ = ' ', space = 0;
  49.  
  50.       uw16 = 0x8000 | ((iptr - --matat) << 3) | (++mlen - 3);
  51.       *optr++ = uw16 >> 8;
  52.       *optr++ = uw16 & 0xFF;
  53.       iptr += mlen;
  54.       continue;
  55.     }
  56.  
  57.     c = *iptr++;
  58.  
  59.     if (c == ' ') {
  60.       if (space)
  61.         *optr++ = ' ';
  62.       space = 1;
  63.       nhc = 0;
  64.       continue;
  65.     }
  66.  
  67.     if (space) {
  68.       space = 0;
  69.       nhc = 0;
  70.       if (c >= 0x40 && c <= 0x7F) {
  71.         *optr++ = c ^ 0x80;
  72.         continue;
  73.       } else
  74.         *optr++ = ' ';          /* couldn't squeeze it in */
  75.     }
  76.  
  77.     if ((c >= 1 && c <= 8) || c >= 0x80) {
  78.       if (nhc) {                /* continue? */
  79.         ++*(optr - ++nhc);
  80.         if (nhc == 8)
  81.           nhc = 0;
  82.       } else {                  /* start */
  83.         *optr++ = '\1';
  84.         nhc = 1;
  85.       }
  86.     } else
  87.       nhc = 0;
  88.  
  89.     *optr++ = c;
  90.  
  91.   }
  92.  
  93.   if (space)
  94.     *optr++ = ' ';              /* add left-over space */
  95.  
  96.   return optr - obuf;
  97. }
  98.